<?php
$loader->requireOnce('includes/Utils.class.php');
class C_Province extends Controller {

	var $template_mod;					
	
	function C_Province($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'Province') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'admin';
		}
		$this->template_mod = $template_mod;		
		$this->view->path = 'province';
		$this->setLayout('admin');
	}	
	
	public function actionViewProvince(){
		return $this->view->render('display.html');
	}
	
	public function actionGetAllProvinces(){
		$pro = Celini::newORDO('Province');
		$province_list = $pro->getAllProvinces();
		return $this->buildProvinceTableResponse($province_list);
	}
	
	public function actionGetAllProvincesSelect(){
		$response = '';
		$pro = Celini::newORDO('Province');
		$province_list = $pro->getAllProvinces();
		//print_r($province_list);
		if(isset($province_list) && $province_list !=''){
			foreach ($province_list as $province){
				$response = $response.$this->buildProvinceSelectResponse($province);
			}
		} else{
			$response = "<option value='empty'>--" . L10nHelper::_("None") . "--</option>";
		}
		return $response;
	}
	
	private function buildProvinceSelectResponse($province){
		$response = "<option id='".$province['province_id']."' name='".$province['name']."' value='".$province['province_id']."'>".$province['name']."</option>";
		return $response;
	}
	
	public function actionAddProvince(){
		$province = Celini::newORDO('Province');			
		$new_name = Utils::sanitizeAndCaseTitle($_POST['name']);
		if($province->checkDuplicateName($new_name)){
			return 1;
		}
		$province->addProvince($new_name);
		$newProvinceList = $province->getAllProvinces();
		// Build response		
		return $this->buildProvinceTableResponse($newProvinceList);
	}
	
	public function actionSaveProvince(){
		$province = Celini::newORDO('Province', $_POST['provinceId']);
		$newName = Utils::sanitizeAndCaseTitle($_POST['name']);		
		if($province->checkDuplicateName($newName)){
			return 1;
		}	
		$province->updateProvince($newName, $_POST['provinceId']);				
		return $newName;
	}
	
	private function buildProvinceTableResponse($province_list){
		$response = '';
		if(isset($province_list) && !empty($province_list)){
			$i = 1;
			foreach($province_list as $province){
				$response .= "<tr id='".$province['province_id']."'>".
				 				"<td align='center'>$i</td>".
								"<td><span id='province_".$province['province_id']."_name_span'>".$province['name']."</span><input style='display:none' type='text' id='province_".$province['province_id']."_name_input' value=''></td>".
								"<td align='center'><a id='province_".$province['province_id']."_edit' href='javascript:editProvince(".$province['province_id'].")'>" . L10nHelper::_("Edit") . "</a>".
								"<a id='province_".$province['province_id']."_save' style='display:none' href='javascript:saveProvince(".$province['province_id'].")'>" . L10nHelper::_("Save") . "</a></td>".
								"<td><a id='province_".$province['province_id']."_cancel' style='display:none' href='javascript:cancelProvince(".$province['province_id'].")'>" . L10nHelper::_("Cancel") . "</a></td>".						
							"</tr>";
				$i++;
			}
		} else {
		    $response = "<tr><td colspan='4'>" . L10nHelper::_("There is no data") . "</td></tr>";
		}
		return $response;	
	}
	
	// District's funciton
	public function actionGetAllDistricts(){
		$district = Celini::newORDO('District');
		$district_list = $district->getAllDistricts($_GET['provinceId']);
		return $this->buildDistrictTableResponse($district_list);
	}
	
	public function actionAddDistrict(){
		$district = Celini::newORDO('District');
		$newName = Utils::sanitizeAndCaseTitle($_POST['name']);				
		if($district->checkDuplicateName($newName, $_POST['provinceId'])){
			return 1;
		}
		$district->addDistrict($newName, $_POST['provinceId']);
		$newDistrictList = $district->getAllDistricts($_POST['provinceId']);
		// Build response		
		return $this->buildDistrictTableResponse($newDistrictList);
	}
	
	public function actionSaveDistrict(){
		$district = Celini::newORDO('District', $_POST['districtId']);
		$newName = Utils::sanitizeAndCaseTitle($_POST['name']);					
		
		if($district->checkDuplicateName($newName, $_POST['provinceId'])){
			return 1;
		}        
		$district->updateDistrict($newName, $_POST['districtId']);				
		return $newName;				
	}

	private function buildDistrictTableResponse($district_list){
		$response = '';
		
		if(isset($district_list) && !empty($district_list)){
			$i = 1;
			foreach($district_list as $district){	
							
				$response .= "<tr id='".$district['district_id']."'>".
								"<td align='center'>$i</td>".
				 				"<td><span id='district_".$district['district_id']."_name_span'>".$district['name']."</span><input style='display:none' type='text' id='district_".$district['district_id']."_name_input' value=''></td>".
								"<td align='center'><a id='district_".$district['district_id']."_edit' href='javascript:editDistrict(\"".$district['district_id']."\")'>" . L10nHelper::_("Edit") . "</a>".
								"<a id='district_".$district['district_id']."_save' style='display:none' href='javascript:saveDistrict(\"".$district['district_id']."\")'>" . L10nHelper::_("Save") . "</a></td>".
								"<td><a id='district_".$district['district_id']."_cancel' style='display:none' href='javascript:cancelDistrict(\"".$district['district_id']."\")'>" . L10nHelper::_("Cancel") . "</a></td>".						
							"</tr>";
				$i++;
			}
		} else {
		    $response = "<tr><td colspan='4'>" . L10nHelper::_("There is no data") . "</td></tr>";
		}
		return $response;	
	}
	
// Ward's funciton
	public function actionGetAllDistrictsSelect(){
		$response = '';
		$district_ordo = Celini::newORDO('District');
		$district_list = $district_ordo->getAllDistricts($_GET['provinceId']);		
		//print_r($district_list);
		if(isset($district_list) && count($district_list) > 0){
			foreach ($district_list as $district){
				$response = $response.$this->buildDistrictSelectResponse($district);
			}
		}else{
			$response .= "<option value='empty'>--" . L10nHelper::_("None") . "--</option>";
		}
		return $response;
	}
	public function actionGetAllWards(){
		$ward_ordo = Celini::newORDO('Ward');
		$ward_list = $ward_ordo->getAllWards($_GET['districtId']);
		
		return $this->buildWardTableResponse($ward_list);		 
	}
	
	public function actionAddWard(){
		$wardOrdo = Celini::newORDO('Ward');	
		$newName = Utils::sanitizeAndCaseTitle($_POST['name']);
		$newCode = strtoupper(Utils::sanitizeText($_POST['code']));
		
		if($wardOrdo->checkDuplicateName($newName, $_POST['districtId'])){
			return 1;
		}
		$wardOrdo->addWard($_POST['districtId'], $newName, $newCode);
		$wardList = $wardOrdo->getAllWards($_POST['districtId']);
		// Build response		
		
		return $this->buildWardTableResponse($wardList);		
	}
	
	public function actionSaveWard(){
		$wardOrdo = Celini::newORDO('Ward', $_POST['wardId']);
		$newName = Utils::sanitizeWard($_POST['name']);
		
		$newCode = strtoupper(Utils::sanitizeText($_POST['code']));				
		if ($wardOrdo->checkDuplicateName($newName, $newCode, $_POST['districtId'])) {		
			return 1;
		}
		$wardOrdo->updateWard($_POST['wardId'], $newName, $newCode);		
		return json_encode(array(
		      'name'  => $newName,
		      'code'  => $newCode)
		);
	}

	private function buildWardTableResponse($ward_list){
	    $response ='';	    
	    if (isset($ward_list) && !empty($ward_list)){
			$i = 1;			
			foreach($ward_list as $ward){
			   
		        $response .= "<tr id='".$ward['ward_id']."'>
						<td align='center'>$i</td>
		 				<td>
		 					<span id='ward_" . $ward['ward_id'] . "_name_span'>" . $ward['name'] . "</span>
		 					<input style='display:none' type='text' id='ward_".$ward['ward_id']."_name_input' value=''>
		 				</td>
		                <td align='center'>
		                	<span id='ward_" . $ward['ward_id'] . "_code_span'>" . $ward['auto_suggest_code'] . "</span>
		 					<input style='display:none; width: 70px;' type='text' id='ward_" . $ward['ward_id'] . "_code_input' value=''>		 					 		                    
						<td align='center'>
							<a id='ward_".$ward['ward_id']."_edit' href='javascript:editWard(".$ward['ward_id'].")'>" . L10nHelper::_("Edit") . "</a>
							<a id='ward_".$ward['ward_id']."_save' style='display:none' href='javascript:saveWard(".$ward['ward_id'].")'>" . L10nHelper::_("Save") . "</a>
						</td>
						<td>
							<a id='ward_".$ward['ward_id']."_cancel' style='display:none' href='javascript:cancelWard(".$ward['ward_id'].")'>" . L10nHelper::_("Cancel") . "</a>
						</td>".						
					"</tr>";		
				$i++;
			} 
		} else {
		     $response = "<tr><td colspan='5'>" . L10nHelper::_("There is no data") . "</td></tr>";
		}
				
		return $response;    			
	}
	
	private function buildDistrictSelectResponse($district){
		$response = "<option id='".$district['district_id']."' name='".$district['name']."' value='".$district['district_id']."'>".$district['name']."</option>";
		return $response;
	}
}
?>